package com.example.myapplication;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {

    Timer timer = null;
    TimerTask task = null;
    private int i = 0;
    private int djs_number = 0;
    String[] musics = {"test01", "tset02", "test03", "test04","test05"};
    int musics_index = 0;
    TextView showtime;  // 显示时间倒计时
    EditText Countdown; //输入倒计时
    EditText number;    //输入倒计时次数
    Button reset;// 重置：reset
    Button begin;// 开始：begin
    Button pause;// 暂停：pause

    Button MusicReset;  // 音乐重置
    Button MusicSuspend;// 音乐暂停
    Button MusicStart;  // 音乐开始
    Button switchover;  // 下一首
    //声明一个MediaPlayer引用
    private MediaPlayer player = null;


    private void init() {
//-----------------标签
//        显示时间倒计时
        showtime = (TextView) findViewById(R.id.Countdown_display);
//-----------------输入窗口
//        输入倒计时
        Countdown = (EditText) findViewById(R.id.Countdown);
//        输入倒计时次数
        number = (EditText) findViewById(R.id.number);
//------------------按钮
//        重置
        reset = (Button) findViewById(R.id.reset);
//        开始
        begin = (Button) findViewById(R.id.begin);
//        暂停
        pause = (Button) findViewById(R.id.pause);

//-----------------音乐按钮
        MusicReset = (Button) findViewById(R.id.MusicReset);
        MusicSuspend = (Button) findViewById(R.id.MusicSuspend);
        MusicStart = (Button) findViewById(R.id.MusicStart);
        switchover = (Button) findViewById(R.id.switchover);

    }

    public void xz() {
        switch (musics_index) {
            case 0:
                player = MediaPlayer.create(MainActivity.this, R.raw.test01);
                break;
            case 1:player = MediaPlayer.create(MainActivity.this, R.raw.test02);
                break;
            case 2:player = MediaPlayer.create(MainActivity.this, R.raw.test03);
                break;
            case 3:player = MediaPlayer.create(MainActivity.this, R.raw.test04);
                break;
            case 4:player = MediaPlayer.create(MainActivity.this, R.raw.test05);
        }
        player.setLooping(true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        xz();
        init();
        switchover.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (musics_index < musics.length - 1) {
                    musics_index++;
                } else
                    musics_index = 0;
                player.reset();
                xz();
                player.start();
            }
        });
        //创建音乐播放器对象
        MusicStart.setOnClickListener(new View.OnClickListener() {
            @Override//音乐开始 MusicStart
            public void onClick(View v) {
                player.start();
            }
        });
        MusicSuspend.setOnClickListener(new View.OnClickListener() {
            @Override//音乐暂停 MusicSuspend
            public void onClick(View v) {
                player.pause();
            }
        });
        MusicReset.setOnClickListener(new View.OnClickListener() {
            @Override//音乐重置：MusicReset
            public void onClick(View v) {
                player.reset();
                xz();
                player.setLooping(true);
            }
        });
        begin.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(djs_number!=1&&i!=0){
                    startTime();
                }else{
                    if (!Countdown.getText().toString().isEmpty()) {
                        if(!Countdown.getText().toString().isEmpty()){
                            i = Integer.parseInt(Countdown.getText().toString());
                            if(number.getText().toString().isEmpty()){
                                djs_number=1;
                            }else{
                                djs_number = Integer.parseInt(number.getText().toString());
                            }
                            showtime.setText("剩余" + djs_number + "次：" + Countdown.getText().toString() + "s");
                            startTime();
                        }



                    }
                }

            }
        });
        pause.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                if(timer!=null){
                    timer.cancel();
                }
                if(player.isPlaying())
                    player.pause();
            }
        });
        reset.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                showtime.setText("");
                timer.cancel();
            }
        });
    }

    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            showtime.setText("剩余" + djs_number + "次：" + msg.arg1 + "s");
            startTime();
        }
    };


    // 显示倒计时
    public void startTime() {
        timer = new Timer();
        task = new TimerTask() {
            @Override
            public void run() {
                i--;
                Message message = mHandler.obtainMessage();
                message.arg1 = i;
                mHandler.sendMessage(message);
//                nHandler.sendMessage(message);
            }
        };
        // 第二个参数表示以1000毫秒计时
        timer.schedule(task, 1000);



        if(i>0&&i<10){
            player.start();
        }else if(i==0){
            i = Integer.parseInt(Countdown.getText().toString());
            player.pause();
            djs_number--;
            if(djs_number==1){
                player = MediaPlayer.create(MainActivity.this, R.raw.test05);
                player.start();
            }else if(djs_number==0){
                timer.cancel();
//                player.pause();
            }
        }






//
//        if(djs_number>1&&(i<10&&i>0)){
//            player.start();
//        }else if(djs_number==1&&i>0){
//            player = MediaPlayer.create(MainActivity.this, R.raw.test05);
//            player.start();
//        }else if(djs_number>1&&i==0){
//            player.start();
//            djs_number--;
//        }else if(djs_number==1&&i==0){
//            timer.cancel();
//            player.pause();
//        }

//        if (djs_number==1&&i>0){//最后一次，直接全部播放
//            player = MediaPlayer.create(MainActivity.this, R.raw.test05);
//            player.start();
//        }
//        if (djs_number!=1&&i <= 10) {
//            player.start();
//        }
//        if (i == 0 && djs_number > 1) {
//            i = Integer.parseInt(Countdown.getText().toString());
//            djs_number--;
//            if(i>10)
//                player.pause();
//
//        }
//
//        if (djs_number == 1&& i == 0) {
//            timer.cancel();
//            player.pause();
//        }

    }


}